//
//  WholeAssessmentViewManager.m
//  智云题库
//
//  Created by 毕武侠 on 2017/3/28.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "WholeAssessmentViewManager.h"
#import "WholeAssessmentModel.h"

@interface WholeAssessmentViewManager ()

@property (nonatomic, strong) YYCache *cache;

@property (nonatomic, strong) WholeAssessmentModel *model;

@end

@implementation WholeAssessmentViewManager
@dynamic model;

- (void)setupConfigure {
    self.cache = [YYCache cacheWithName:@"AssessmentCache"];
    WholeAssessmentModel *model = (WholeAssessmentModel *)[self.cache objectForKey:@"WholeAssessmentModel"];
    if (model) {
        self.model = model;
        self.view.hidden = NO;
    } else {
        self.view.hidden = YES;
    }
}

- (void)setDataSignal:(RACSignal *)dataSignal {
    _dataSignal = dataSignal;
    
    [_dataSignal subscribeNext:^(id x) {
        if ([x isKindOfClass:[WholeAssessmentModel class]]) {
            self.model = x;
            [self.cache setObject:self.model forKey:@"WholeAssessmentModel"];
            self.view.hidden = NO;
            [self refreshView];
        }
    }];
}

- (void)refreshView {
    @try {
        
        UILabel *forecastFen = (UILabel *)[self.view viewWithTag:10];
        forecastFen.text = self.model.avg_point;
        
        UILabel *otherAvgFen = (UILabel *)[self.view viewWithTag:11];
        otherAvgFen.text = self.model.examinee_avg;
        
        UILabel *beatOhterRate = (UILabel *)[self.view viewWithTag:12];
        beatOhterRate.text = self.model.beat;
        
        UILabel *answerTime = (UILabel *)[self.view viewWithTag:13];
        answerTime.text = self.model.time;
        
        UILabel *answerSum = (UILabel *)[self.view viewWithTag:14];
        answerSum.text = self.model.answer_sum;
        
        UILabel *answerRight = (UILabel *)[self.view viewWithTag:15];
        answerRight.text = self.model.right_answer_sum;
        
        UILabel *answerRate = (UILabel *)[self.view viewWithTag:16];
        answerRate.text = [NSString stringWithFormat:@"%@%@",self.model.right_rate,@"%"];
        
        UILabel *forecastTopFen = (UILabel *)[self.view viewWithTag:17];
        forecastTopFen.text = self.model.high_score;
        
        UILabel *myRank = (UILabel *)[self.view viewWithTag:18];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@/%@", self.model.my_ranking, self.model.people_total]];
        
        [str addAttribute:NSForegroundColorAttributeName value:RGB(255, 30, 0) range:NSMakeRange(0, self.model.my_ranking.length)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(self.model.my_ranking.length,self.model.people_total.length + 1)];
        myRank.attributedText = str;
        
        PNLineChart *lineChart = (PNLineChart *)[self.view viewWithTag:19];
        lineChart.showLabel = YES;
        lineChart.showYGridLines = YES;
        lineChart.showGenYLabels = YES;
        lineChart.showCoordinateAxis = YES;
        
        lineChart.axisColor = [UIColor darkGrayColor];
        lineChart.xLabelColor = [UIColor darkGrayColor];
        lineChart.yLabelColor = [UIColor darkGrayColor];
        
        [lineChart setXLabels:self.model.t_time];
    //    [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];
    //    NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2];
        PNLineChartData *data01 = [PNLineChartData new];
        data01.color = MainThemeColor;
        data01.itemCount = lineChart.xLabels.count;
        data01.getData = ^(NSUInteger index) {
            CGFloat yValue = [self.model.t_point[index] floatValue];
            return [PNLineChartDataItem dataItemWithY:yValue];
        };
        
        lineChart.chartData = @[data01];
        [lineChart strokeChart];
    } @catch (NSException *exception) {
        NSLog(@"exception:%s,%@",__func__,exception);
    } @finally {
        
    }

}

@end
